﻿<?php
class Model_User extends Zend_Db_Table_Abstract
{
		protected $_name = 'user';
		protected $_primary = 'u_email';
		public function createUser ($hoten, $ngaysinh, $dienthoai, $diachi, $email,$password)
		{
			$rowUser = $this->createRow();
			if ($rowUser) {
				// update the row values
				$rowUser->u_hoten = $hoten;
				$rowUser->u_ngaysinh = $ngaysinh;
				$rowUser->u_dienthoai = $dienthoai;
				$rowUser->u_diachi = $diachi;
				$rowUser->u_email = $email;
				$rowUser->u_password = md5($password);
				$rowUser->save();
				//return the new user
				return $rowUser;
			} else {
				throw new Zend_Exception("Không thể tạo được người dùng! ");
			}
		}
		
	public static function getUsers()
	{
		$userModel = new self();
		$select = $userModel->select();
		$select->order(array('u_hoten'));
		return $userModel->fetchAll($select);
	}

	public function updateUser($hoten, $ngaysinh, $dienthoai,$diachi,$email)
	{
		// fetch the user's row
		$rowUser = $this->find($email)->current();
		//var_dump($rowUser);
		if($rowUser) {
			// update the row values
			$rowUser->u_hoten = $hoten;
			$rowUser->u_ngaysinh = $ngaysinh;
			$rowUser->u_dienthoai = $dienthoai;
			//$rowUser->u_email = $email;
			$rowUser->u_diachi = $diachi;
			$rowUser->save();
			//return the updated user
			return $rowUser;
		}else{
			throw new Zend_Exception("Không cập nhật được.  Không tìm thấy người sử dụng!");
		}
	}
	public function updatePassword($email, $password)
	{
		// fetch the user's row
		$rowUser = $this->find($email)->current();

		if($rowUser) {
			//update the password
			$rowUser->u_password = md5($password);
			$rowUser->save();
		}else{
			throw new Zend_Exception("Password update failed.  User not found!");
		}
	}
	
	public function updateRole($id,$role)
	{
		$where = $this->getAdapter()->quoteInto('u_id =?',$id);
		$set = array('u_role' =>$role);
		$rows_affected = $this->update($set, $where);
	}
	public function deleteUser($email)
	{
		// fetch the user's row
		$rowUser = $this->find($email)->current();
		if($rowUser) {
			$rowUser->delete();
		}else{
			throw new Zend_Exception("Could not delete user.  User not found!");
		}
	}
	
	
	public function getUserById($uid)
	{
		$userModel = new self();
		$select = $userModel->select();
		$select->where('u_id=?',$uid);
		return $userModel->fetchAll($select);
	
	}
	
	public static function getUserLimit($start,$count)
	{
		$userModel = new self();
		$select = $userModel->select();
		$select->order(array('u_id'))
			   ->limit($start,$count);	
		return $userModel->fetchAll($select);
	}
	public function deleteUserById($uid)
	{
		
	}

}
